package cn.iocoder.yudao.module.biz.controller.admin.risk.riskType;

import cn.iocoder.yudao.module.biz.controller.admin.risk.riskType.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.riskType.BizRiskTypeConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskType.BizRiskTypeDO;
import cn.iocoder.yudao.module.biz.service.risk.riskType.BizRiskTypeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 风险类型")
@RestController
@RequestMapping("/biz/riskType")
@Validated
public class BizRiskTypeController {

    @Resource
    private BizRiskTypeService riskTypeService;

    @PostMapping("/create")
    @ApiOperation("创建风险类型")
    @PreAuthorize("@ss.hasPermission('biz:risk-type:create')")
    public CommonResult<Boolean> createRiskType(@Valid @RequestBody BizRiskTypeCreateReqVO createReqVO) {
        riskTypeService.createRiskType(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @ApiOperation("更新风险类型")
    @PreAuthorize("@ss.hasPermission('biz:risk-type:update')")
    public CommonResult<Boolean> updateRiskType(@Valid @RequestBody BizRiskTypeUpdateReqVO updateReqVO) {
        riskTypeService.updateRiskType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除风险类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-type:delete')")
    public CommonResult<Boolean> deleteRiskType(@RequestParam("id") String id) {
        riskTypeService.deleteRiskType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得风险类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-type:query')")
    public CommonResult<BizRiskTypeRespVO> getRiskType(@RequestParam("id") String id) {
        BizRiskTypeDO riskType = riskTypeService.getRiskType(id);
        return success(BizRiskTypeConvert.INSTANCE.convert(riskType));
    }

    @GetMapping("/list")
    @ApiOperation("获得风险类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-type:query')")
    public CommonResult<List<BizRiskTypeRespVO>> getRiskTypeList(@RequestParam("ids") Collection<String> ids) {
        List<BizRiskTypeDO> list = riskTypeService.getRiskTypeList(ids);
        return success(BizRiskTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得风险类型分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-type:query')")
    public CommonResult<PageResult<BizRiskTypePageReqVO>> getRiskTypePage(@Valid BizRiskTypePageReqVO pageVO) {
        PageResult<BizRiskTypeDO> pageResult = riskTypeService.getRiskTypePage(pageVO);
        return success(BizRiskTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出风险类型 Excel")
    @PreAuthorize("@ss.hasPermission('biz:risk-type:export')")
    @OperateLog(type = EXPORT)
    public void exportRiskTypeExcel(@Valid BizRiskTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizRiskTypeDO> list = riskTypeService.getRiskTypeList(exportReqVO);
        // 导出 Excel
        List<BizRiskTypeExcelVO> datas = BizRiskTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "风险类型.xls", "数据", BizRiskTypeExcelVO.class, datas);
    }

}
